<?php
## include required files
/*******************************/
require_once '../../includefiles.php';
require_once '../../model/trade.php';
include_once("../../model/users.php");
require_once '../../model/state.php';
require_once '../../redirect.php';

## Create Objects
/*******************************/
$objTrade = new Model_TRADE();
$userObj = new Model_Users();
$stateObj = new Model_State();
/*******************************/

checkUserLogin();


if($_SESSION['user_type'] == '3')
{
	@header("location:".SITE_URL."/");
	exit;
}

$userId = $_SESSION['getbids_id'];
$userDetArray = $userObj->getUserDetailsByUserId($userId);
$comapnyname = str_replace(" ","_",strtolower($userDetArray['companyname']));
$userDetArray['newcomapnyname'] =$comapnyname;
$smarty->assign('userDetArray',$userDetArray);

## Fetch all active states
$stateArray = $stateObj->getAllStateWithActiveStatus();
//echo "<pre>"; print_r($stateArray); exit;
//get work area
$workareas = $userObj->getWorkAreas($userId);

for($i=0;$i<count($stateArray);$i++)
{
	if(in_array($stateArray[$i]["state_id"],$workareas))
		$stateArray[$i]["selected"] = 1;
	else
		$stateArray[$i]["selected"] = 0;
		
}

$smarty->assign('stateArray', $stateArray);



if( isset($_POST['googlesearch']) && isset($_POST['state_id'])) 
{
	extract($_POST);
	$userEditArray = array();
	$userEditArray["workarea"]	= $_POST['googlesearch'];
	$userEditArray["modified_date"] = getCurDate();
	$userObj->editUserValueById($userEditArray,$userId);
	
	#-------------- START work area Added for User ------------
	$userObj->deleteWorkAreaValueByUserId($userId);
	
	$printval					= $_POST['state_id'];
	foreach($printval as $per)
	{
		$workArr['user_id']		= $userId;
		$workArr['state_id']	= $per;
		$workArr['added_on']	= getCurDate();
		$workAreaId				= $userObj->addWorkAreaByValue($workArr);
	}
				
	$_SESSION['msg'] = "<div class='success_msg'><span>Services & location updated successfully.</span></div>";
	redirect_to_link(SITE_URL.'/myaccount/myservicelocation/');
	exit;
	
}

$smarty->assign("new", '0');	
if($_SESSION['msg'] != ''){
	$smarty->assign("msg", $_SESSION['msg']);	
	unset($_SESSION['msg']);
}
$usercats = $userObj->getAllCatsbought($userId);
$smarty->assign('usercats',$usercats);


$active = "myaccount";
$smarty->assign("active", $active);
$subactive ="myservicelocation";
$smarty->assign("subactive", $subactive);
$smarty->assign('mainmenu', '3');
$smarty->display(TEMPLATEDIR . '/modules/myaccount/myservicelocation.tpl');
?>